package br.uvv.sao.managedBean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.primefaces.model.DualListModel;
import org.primefaces.model.LazyDataModel;

import br.uvv.sao.domain.PaginasImplementadas;
import br.uvv.sao.domain.Perfil;
import br.uvv.sao.domain.Permissao;
import br.uvv.sao.domain.Usuario;
import br.uvv.sao.enums.SituPerfil;
import br.uvv.sao.rn.PermissaoRN;
import br.uvv.sao.util.MsgUtil;
import br.uvv.sao.util.Util;

public class PermissaoBean implements Serializable {
	private static final long serialVersionUID = -7853119461631940646L;
	
	private PermissaoRN permissaoRN;
	private Usuario usuarioLogado;
	private DualListModel<PaginasImplementadas> permissaoDualList;
	private List<PaginasImplementadas> permissaoSourceList;
	private List<PaginasImplementadas> permissaoTargetList;
	private LazyDataModel<Perfil> perfilList;
	private LazyDataModel<Permissao> permissaoList;
	private Perfil perfilInstance;
	private Perfil perfilInstanceTemp;
	private String nome;
	
	public String permissao(){
		init();
		return "goPermissoes";
	}
	
	public String goHome(){
		return "goHome";
	}
	
	public String editar(){
		if(perfilInstance.getSituacao().equals(SituPerfil.Ativo)){
			listarPermissaoTarget();
			listarPermissaoSource();
			permissaoDualList = new DualListModel<PaginasImplementadas>(permissaoSourceList, permissaoTargetList);
			return "goEdit";
		}else{
			MsgUtil.msgError("Permissões para perfis inativos não podem ser alteradas!");
			return null;
		}
	}
	
	public String desistirEditar(){
		permissaoDualList = new DualListModel<PaginasImplementadas>();
		permissaoSourceList = new ArrayList<PaginasImplementadas>();
		permissaoTargetList = new ArrayList<PaginasImplementadas>();
		listarPermissoes();
		return "goPermissao";
	}
	
	public String salvar(){
		System.out.println("Total destino: " + permissaoDualList.getTarget().size());
		for (PaginasImplementadas pagina : permissaoDualList.getTarget()) {
			System.out.println(pagina.getUrl());
		}
		if(this.permissaoRN.salvar(this.perfilInstance, permissaoDualList.getSource(), permissaoDualList.getTarget())){
			MsgUtil.msgInfo("Permissões salvas com sucesso");
			listarPermissoes();
			return "goPermissao";
		}else{
			MsgUtil.msgError("Erro ao salvar permissões");
			return null;
		}
	}
	
	public void desistir(){
		init();
	}
	
	@SuppressWarnings("serial")
	public void preBuscaPerfil(){
		perfilList = new LazyDataModel<Perfil>() {
			
			@Override
			public List<Perfil> load(int arg0, int arg1, String arg2, boolean arg3,
					Map<String, String> arg4) {
				// TODO Auto-generated method stub
				return new ArrayList<Perfil>();
			}
		};
		perfilList.setPageSize(10);
		perfilInstanceTemp = new Perfil();
		nome = "";
	}
	
	@SuppressWarnings("serial")
	public void listarPerfis(){
		try {
			this.perfilList = new LazyDataModel<Perfil>() {
				
				@Override
				public List<Perfil> load(int first, int pageSize, String sortField, boolean sortOrder,
						Map<String, String> arg4) {
					return permissaoRN.listarPerfis(first, pageSize, sortField, sortOrder, nome);
				}
			};
			this.perfilList.setRowCount(permissaoRN.perfilCount(this.nome));
			this.perfilList.setPageSize(10);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("serial")
	public void selecionarPerfil(){
		try {
			if(this.perfilInstanceTemp.getId() > 0){
				this.perfilInstance = this.perfilInstanceTemp;
				listarPermissoes();
			}
			this.perfilInstanceTemp = new Perfil();
			this.perfilList = new LazyDataModel<Perfil>() {
				
				@Override
				public List<Perfil> load(int arg0, int arg1, String arg2, boolean arg3,
						Map<String, String> arg4) {
					return null;
				}
			};
		} catch (NullPointerException e) {
		}
	}
	
	public void listarPermissaoSource(){
		permissaoSourceList = this.permissaoRN.listarPermissaoSource(perfilInstance, permissaoTargetList);
	}
	
	public void listarPermissaoTarget(){
		permissaoTargetList = this.permissaoRN.listarPermissaoTarget(perfilInstance);
	}
	
	@SuppressWarnings("serial")
	public void listarPermissoes(){
		try {
			System.out.println("veio listar");
			this.permissaoList = new LazyDataModel<Permissao>() {
				
				@Override
				public List<Permissao> load(int first, int pageSize, String sortField, boolean sortOrder,
						Map<String, String> arg4) {
					return permissaoRN.listarPermissoes(first, pageSize, sortField, sortOrder, perfilInstance);
				}
			};
			this.permissaoList.setRowCount(permissaoRN.permissoesCount(this.perfilInstance));
			this.permissaoList.setPageSize(10);
			System.out.println("listou: " + permissaoList.getRowCount());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("serial")
	@PostConstruct
	public void init(){
		this.permissaoRN = new PermissaoRN();
		this.usuarioLogado = Util.getUsuarioLogado();
		this.permissaoSourceList = new ArrayList<PaginasImplementadas>();
		this.permissaoTargetList = new ArrayList<PaginasImplementadas>();
		this.permissaoDualList = new DualListModel<PaginasImplementadas>(permissaoSourceList, permissaoTargetList);
		this.perfilList = new LazyDataModel<Perfil>() {
			
			@Override
			public List<Perfil> load(int first, int pageSize, String sortField, boolean sortOrder,
					Map<String, String> arg4) {
				return new ArrayList<Perfil>();
			}
		};
		this.perfilList.setPageSize(10);
		this.permissaoList = new LazyDataModel<Permissao>() {
			
			@Override
			public List<Permissao> load(int arg0, int arg1, String arg2, boolean arg3,
					Map<String, String> arg4) {
				return new ArrayList<Permissao>();
			}
		};
		this.permissaoList.setPageSize(10);
		this.perfilInstance = new Perfil();
		this.perfilInstanceTemp = new Perfil();
		this.nome = "";
	}
	
	public void destroy(){
		this.permissaoRN = null;
		this.usuarioLogado = null;
		this.permissaoSourceList = null;
		this.permissaoTargetList = null;
		this.permissaoDualList = null;
		this.perfilList = null;
		this.perfilInstance = null;
		this.perfilInstanceTemp = null;
	}
	
	public PermissaoRN getPermissaoRN() {
		return permissaoRN;
	}
	public void setPermissaoRN(PermissaoRN permissaoRN) {
		this.permissaoRN = permissaoRN;
	}
	public Usuario getUsuarioLogado() {
		return usuarioLogado;
	}
	public void setUsuarioLogado(Usuario usuarioLogado) {
		this.usuarioLogado = usuarioLogado;
	}
	public DualListModel<PaginasImplementadas> getPermissaoDualList() {
		return permissaoDualList;
	}
	public void setPermissaoDualList(DualListModel<PaginasImplementadas> permissaoDualList) {
		this.permissaoDualList = permissaoDualList;
	}
	public List<PaginasImplementadas> getPermissaoSourceList() {
		return permissaoSourceList;
	}
	public void setPermissaoSourceList(List<PaginasImplementadas> permissaoSourceList) {
		this.permissaoSourceList = permissaoSourceList;
	}
	public List<PaginasImplementadas> getPermissaoTargetList() {
		return permissaoTargetList;
	}
	public void setPermissaoTargetList(List<PaginasImplementadas> permissaoTargetList) {
		this.permissaoTargetList = permissaoTargetList;
	}
	public LazyDataModel<Perfil> getPerfilList() {
		return perfilList;
	}
	public void setPerfilList(LazyDataModel<Perfil> perfilList) {
		this.perfilList = perfilList;
	}
	public Perfil getPerfilInstance() {
		return perfilInstance;
	}
	public void setPerfilInstance(Perfil perfilInstance) {
		this.perfilInstance = perfilInstance;
	}
	public Perfil getPerfilInstanceTemp() {
		return perfilInstanceTemp;
	}
	public void setPerfilInstanceTemp(Perfil perfilInstanceTemp) {
		this.perfilInstanceTemp = perfilInstanceTemp;
	}
	public String getNome() {
		return nome;
	}
	public void setNome(String nome) {
		this.nome = nome;
	}

	public LazyDataModel<Permissao> getPermissaoList() {
		return permissaoList;
	}

	public void setPermissaoList(LazyDataModel<Permissao> permissaoList) {
		this.permissaoList = permissaoList;
	}
	
}
